package com.qf.boot2202day03.controller;

import com.qf.qfsyspringbootstarter.utils.QfFileAppender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.*;

@RestController
@RequestMapping("/exp")
public class ExportController {

    @Autowired
    QfFileAppender qfFileAppender;

    @Autowired
    ApplicationContext applicationContext;

    @GetMapping("/test1")
    public Map<String,Object> test1() throws IOException {

        final List<String> list = Arrays.asList("luffy", "zero", "nami", "wusupu", "joba");


        final String filename = UUID.randomUUID().toString();

        qfFileAppender.initWriter(filename);

        qfFileAppender.appendListToFile(list);

        qfFileAppender.close();

        final HashMap<String, Object> map = new HashMap<>();
        map.put("code","1");
        map.put("msg","导出成功");

        return map;

    }

    @GetMapping("/test2")
    public Map<String,Object> test2() throws IOException{

        final QfFileAppender appender = applicationContext.getBean(QfFileAppender.class);

        final List<String> list = Arrays.asList("luffy", "zero", "nami", "wusupu", "joba");


        final String filename = UUID.randomUUID().toString();

        appender.initWriter(filename);

        appender.appendListToFile(list);

        appender.close();

        final HashMap<String, Object> map = new HashMap<>();
        map.put("code","1");
        map.put("msg","导出成功");

        return map;

    }

}
